package com.huluobo.greendaodemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.huluobo.greendaodemo.db.DaoSession;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private EditText username, age;
    private TextView query;
    private DaoSession daoSession;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        username = findViewById(R.id.username);
        age = findViewById(R.id.age);
        query = findViewById(R.id.query);
        daoSession = ((App) getApplication()).getDaoSession();
    }

    //增
    public void insert(View view) {
        User user = new User(null, username.getText().toString(), Integer.parseInt(age.getText().toString()));
        daoSession.insert(user);
    }

    //查
    public void queryAll(View view) {
        List<User> users = daoSession.loadAll(User.class);
        for (int i = 0; i < users.size(); i++) {
            query.setText(users.get(i).toString());
        }
    }
}